<?php

/*
 * This class is used for as Model for Customer table 
 */

class Model_Customer extends Model_Base {

    protected $_name = 'tbl_customer';
    protected $_primaryKey = 'cust_id';

    /* stored value in session when customer login */

    public function getCustomerAuthData($dataArray = Array()) {
        $select = $this->select()
                ->from(array('lgn' => 'tbl_login'))
                ->setIntegrityCheck(false)
                ->join(array('cus' => 'tbl_customer'), "lgn.lgn_id = cus.lgn_id")
                ->where("cus.lgn_id = '" . $dataArray['lgn_id'] . "'")
                ->where("lgn.lgn_id = '" . $dataArray['lgn_id'] . "'");
        $data = parent::fetchRow($select);

        //for language code
        $languageModel = new Model_Language();
        $where = 'lang_id=' . $data->lang_id;
        $languagedefaultArray = $languageModel->fetchType($where);
        if ($data->cust_isadmin == 1) {
            $roletype = 'SuperCustomer';
        } else {
            $roletype = 'Customer';
        }
        $object = new stdClass();
        $object->usr_id = $data->cust_id;
        $object->lgn_id = $data->lgn_id;
        $object->lgn_email = $data->lgn_email;
        $object->lgn_type = $data->lgn_type;
        $object->name = $data->cust_firstname . " " . $data->cust_lastname;
        $object->role = "CUSTOMER";
        $object->roletype = $roletype;
        $object->lgn_active = $data->lgn_active;
        $object->company_id = $data->com_id;
        $object->usr_lang_id = $data->lang_id;
        $object->usr_lang_code = strtolower($languagedefaultArray[0]['lang_code']);
        $object->COMPANY_SESSION_ID = "";
        return $object;
    }

    
     //getdepartmentArray
    public function getdepartmentArray($where = '') {
        $departmentModel = new Model_Department();
        $departmentArray = $departmentModel->fetchDepartment($where);
         foreach ($departmentArray as $com) {
            $finalArray[$com['dep_id']] = $com['dep_name'] . " [".$com['site_name']."]";
        }
        if (!empty($finalArray)) {
            return $finalArray;
        } else {
            return array();
        }
       
    }
    
    
    //get all languages
    public function getlanguageArray() {
        $languageModel = new Model_Language();
        $languageArray = $languageModel->language_fetchall();
        $where = 'lang_isdefault=1';
        $languagedefaultArray = $languageModel->fetchType($where);
        $finalArray[$languagedefaultArray[0]['lang_id']] = $languagedefaultArray[0]['lang_name'] . " [" . strtolower($languagedefaultArray[0]['lang_code']) . "]";
        foreach ($languageArray as $com) {
            $finalArray[$com['lang_id']] = $com['lang_name'] . " [" . strtolower($com['lang_code']) . "]";
        }
        return $finalArray;
    }

    //get data of customer if login type CUSTOMER
    public function getcustomerId($lgn_id) {
        $select = $this->select()
                ->from(array('cust' => 'tbl_customer'))
                ->setIntegrityCheck(false)
                ->join(array('lgn' => 'tbl_login'), "lgn.lgn_id = cust.lgn_id")
                ->where('cust.lgn_id = ?', $lgn_id)
                ->where('lgn_active != 2');
        // see reasoning in fetchEntries() as to why we return only an array
        #echo $select;exit;    
        $data = $this->fetchRow($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }

    //get company data
    public function getcompanyArray($where = "") {
        $companyModel = new Model_Company();
        $companyArray = $companyModel->fetchCompany($where);
        $finalArray[''] = '';
        foreach ($companyArray as $com) {
            $finalArray[$com['com_id']] = $com['com_name'];
        }


        return $finalArray;
    }

    //Get Quality data from tbl_list table
    public function getqualityArray($lang_id) {
        $ParentdropdownModel = new Model_Parentlist();
        $ParentdropdownArray = $ParentdropdownModel->fetchall('Quality');
        $finalArray[''] = '-- Select Quality --';
        $parent_id = $ParentdropdownArray[0]['parent_id'];
        $ChilddropdownModel = new Model_List();
        $where = 'child.lang_id = '.$lang_id.' and child.parent_id = '.$parent_id.' ';
        $ChilddropdownArray = $ChilddropdownModel->fetchType($where);

        foreach ($ChilddropdownArray as $com) {
            $finalArray[$com['ref_child_id']] = $com['child_name'];
        }
        return $finalArray;
    }

    
    //Get title data from tbl_list table

    public function gettitleArray($lang_id) {
        $ParentdropdownModel = new Model_Parentlist();
        $ParentdropdownArray = $ParentdropdownModel->fetchall('Salutations_title');
        $finalArray[''] = '-- Select Title --';
        $parent_id = $ParentdropdownArray[0]['parent_id'];
        $ChilddropdownModel = new Model_List();
        $where = 'child.lang_id = '.$lang_id.' and child.parent_id = '.$parent_id.' ';
        $ChilddropdownArray = $ChilddropdownModel->fetchType($where);

        foreach ($ChilddropdownArray as $com) {
            $finalArray[$com['ref_child_id']] = $com['child_name'];
        }
        if (!empty($finalArray)) {
            return $finalArray;
        } else {
            return array();
        }
    }
    //Get availability data from tbl_list table
    public function getavailabilityArray($lang_id) {

        $ParentdropdownModel = new Model_Parentlist();
        $ParentdropdownArray = $ParentdropdownModel->fetchall('Availability');
        $finalArray[''] = '-- Select Availability --';
        $parent_id = $ParentdropdownArray[0]['parent_id'];
        $ChilddropdownModel = new Model_List();
        $where = 'child.lang_id = '.$lang_id.' and child.parent_id = '.$parent_id.' ';
        $ChilddropdownArray = $ChilddropdownModel->fetchType($where);

        foreach ($ChilddropdownArray as $com) {
            $finalArray[$com['ref_child_id']] = $com['child_name'];
        }
        return $finalArray;
    }

    //get country data
    public function getcountryArray() {
        $countryModel = new Model_Country();
        $countryArray = $countryModel->fetchall();
        $finalArray[''] = '';
        foreach ($countryArray as $com) {
            $finalArray[$com['country_id']] = $com['country_name'];
        }
        return $finalArray;
    }

    /**
     * Function to return different status of the Customer
     *
     * @param  null
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function getStatusArray() {
        return array(
            #'' => '-- Select Status --',
            '1' => 'Active',
            '0' => 'Inactive'
        );
    }

    //get data of admin if login type ADMIN
    public function getadminId($lgn_id) {
        $select = $this->select()
                ->from(array('cust' => 'tbl_customer'))
                ->setIntegrityCheck(false)
                ->join(array('lgn' => 'tbl_login'), "lgn.lgn_id = cust.lgn_id")
                ->where('cust.lgn_id = ?', $lgn_id)
                ->where('lgn_active != 2');
        $data = $this->fetchRow($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }

    /**
     * Fetch an individual entry for view
     *
     * @param  int|string $id
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function fetchEntryByViewId($id,$lang_id) {
        //array('child_quality' => 'child_name')        
        $select = $this->select()
                ->setIntegrityCheck(false)
                ->from(array('cust' => 'tbl_customer'))
                ->join(array('lgn' => 'tbl_login'), "lgn.lgn_id = cust.updated_lgn_id")
                ->join(array('lgn_cust' => 'tbl_login'), "lgn_cust.lgn_id = cust.lgn_id", array('lgn_cust_email' => 'lgn_email'))
                ->join(array('country' => 'tbl_country'), "country.country_id = cust.cust_country")
                ->join(array('company' => 'tbl_company'), "company.com_id = cust.com_id")
                ->joinLeft(array('title' => 'tbl_list'), "title.child_id = cust.cust_title and title.lang_id = ".$lang_id."", array('cust_title_child' => 'child_name'))
                ->joinLeft(array('child' => 'tbl_list'), "child.child_id = cust.cust_quality and child.lang_id = ".$lang_id."", array('child_quality' => 'child_name'))
                ->joinLeft(array('child1' => 'tbl_list'), "child1.child_id = cust.cust_availability and child1.lang_id = ".$lang_id."", array('child_availability' => 'child_name'))
                ->where('cust_id = ?', $id)
                ->where('lgn.lgn_active != 2');
        // see reasoning in fetchEntries() as to why we return only an array
        #echo $select;exit;
        $data = $this->fetchRow($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }

    public function fetchEntryById($id) {
        $select = $this->select()
                ->from(array('cust' => 'tbl_customer'))
                ->setIntegrityCheck(false)
                ->join(array('lgn' => 'tbl_login'), "lgn.lgn_id = cust.lgn_id")
                ->where('cust_id = ?', $id)
                ->where('lgn_active != 2');
        // see reasoning in fetchEntries() as to why we return only an array

        $data = $this->fetchRow($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }

    /**
     *  This function is used to get customers
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function fetchCustomer($lang_id ,$where = "", $limit = "", $offset = "0", $order = 'cust_id', $sort = 'ASC') {
        $select = $this->select()
                ->from(array('cust' => 'tbl_customer'), Array('*', 'customer_name' => "CONCAT(cust_firstname , ' ', cust_lastname)"))
                ->setIntegrityCheck(false)
                ->joinLeft(array('title' => 'tbl_list'), "title.child_id = cust.cust_title and title.lang_id =".$lang_id." ", array('cust_title_child' => 'child_name'))
                ->join(array('lgn' => 'tbl_login'), "lgn.lgn_id = cust.lgn_id")
                ->join(array('cmp' => 'tbl_company'), "cmp.com_id = cust.com_id")
                ->where('lgn_active != 2');
        if ($where != '') {
            $select->where($where);
        }

        if ($limit != "") {
            $select->limit($limit, $offset);
        }

        $select->order($order . " " . $sort);
        #echo $select; exit;
        $data = parent::fetchAll($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }

    /**
     *  This function is used to get total rows
     * @return int
     */
    public function getCustomerCount($lang_id,$where) {
        $select = $this->select()
                ->from(array('cust' => 'tbl_customer'), Array('cnt' => 'count(*)'))
                ->setIntegrityCheck(false)
                ->joinLeft(array('title' => 'tbl_list'), "title.child_id = cust.cust_title and title.lang_id =".$lang_id."", array('cust_title_child' => 'child_name'))
                ->join(array('lgn' => 'tbl_login'), "lgn.lgn_id = cust.lgn_id")
                ->join(array('cmp' => 'tbl_company'), "cmp.com_id = cust.com_id")
                ->where('lgn_active != 2');
        if ($where != '') {
            $select->where($where);
        }
        #echo $select;exit;
        $rows = parent::fetchRow($select);
        return($rows->cnt);
    }

    /**
     *  This function is used to delete customers
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function deleteCustomer($prdtArray) {
        $loginModel = new Model_Login();
        $config = Zend_Registry::get('config');
        if (is_array($prdtArray)) {
            foreach ($prdtArray as $_customer) {
                $customerData = Array();
                $customerData = $this->fetchEntryById($_customer);
                if (count($customerData) > 0) {
                    //update status in login table 
                    $login_delete = $loginModel->deleteloginid($customerData['lgn_id']);
                    //delete images
                    $folder = $config->UPLOAD_PATH . "customer/";
                    $customer_image = $folder . $customerData['cust_image'];
                    if (file_exists($customer_image)) {
                        @unlink($customer_image);
                    }
                }
            }
        } else {
            $cust_id = $prdtArray;
            $customerData = $this->fetchEntryById($cust_id);
            if (count($customerData) > 0) {
                //update status in login table 
                $login_delete = $loginModel->deleteloginid($customerData['lgn_id']);
                //delete images
                $folder = $config->UPLOAD_PATH . "customer/";
                $customer_image = $folder . $customerData['cust_image'];
                if (file_exists($customer_image)) {
                    @unlink($customer_image);
                }
            }
        }
    }
    
    public function getcustomerArray($lang_id,$where = "") {

        $customerArray = $this->fetchCustomer($lang_id,$where);
        $finalArray[''] = 'Select Customer';
        if ($where != '') {
            foreach ($customerArray as $com) {
                $finalArray[$com['cust_id']] = ucfirst($com['cust_firstname'] . " " . $com['cust_lastname']);
            }
        }
        return $finalArray;
    }

}
